<?php
namespace app\index\controller\project;

use app\index\controller\Base;

class Team extends base_project
{
    public $tableName = "Team";
    public function index()
    {
        $map["project_id"] = $this->base_project_id;
        $search=input("search");
        if(!empty($search)){
            $map["name|phone"]=array("like","%".trim($search)."%");
        }
     
    
        $roles=model("Role")->all()->toArray();
        $role_item=array_column($roles,"name","id");
        $this->page($map,12,"","id desc",["user","project"]);
        $data["role_item"]=$role_item;
        $this->assign($data);
        return view();
    }

    public function project()
    {   
        $model = model('Team');
  
        $map["project_id"] = $this->base_project_id;
     
        $data_list = $model->where($map)->with(["user","project"])->order("id desc")->paginate(10);
     
        $data["data_list"] = $data_list;
        $data["page"] = $data_list->render();
        $this->assign($data);
        return view();
    }
    /**
     *  添加
     */
    public function add()
    {
        $model = model('Team');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
            $url="/Index/Project/Team/index";
            $prame=[];          
            $data["phone"] = $post["phone"];
            $data["email"] = $post["email"];
            $data["duty"] = $post["duty"];   
            if (isset($post["project_id"])) {
                $data["project_id"] = $post["project_id"];
                $url="/Index/Project/Team/index";
                $prame["project_id"]= $post["project_id"];
            }
            $data["user_id"] = $post["user_id"];

            $TeamModel = model("Team");
            $result = $TeamModel->save($data);
            if (empty($result)) {
                $this->error("操作失败");

            }

            $this->success("操作成功", url($url,$prame));
        }
        // $model=model("Admin");
        //$data=[];
        $data["users"] = model("Admin")->where(["role_id" => 4])->field("id,username,nickname,nickname,phone,email,role_id")->select();
        $this->assign($data);
        return view();
    }

    /**
     *  修改
     */
    public function edit()
    {
        $TeamModel = model('Team');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
          
            $post = request()->instance()->param();
            $url="/Index/Project/Team/index";
            $prame=[];          
            $data["phone"] = $post["phone"];
            $data["email"] = $post["email"];
            $data["duty"] = $post["duty"];   
            if (isset($post["project_id"])) {
                $data["project_id"] = $post["project_id"];
                $url="/Index/Project/Team/index";
                $prame["project_id"]= $post["project_id"];
            }
            $map["id"] = $post["id"];
            $result = $TeamModel->save($data, $map);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url($url,$prame));
        }

        $id = input("id");
        $project=model("Project")->where(["id"=>input('project_id')])->field("id,title")->find();
        $data["project"] =$project;
        $data["info"] = $TeamModel->get($id);
        $data["user"]=model("admin")->where(['id'=>$data["info"]["user_id"]])->field("id,nickname")->find();
        $this->assign($data);
        return view();
    }

    public function del()
    {
        $id = input("id");
        if (empty($id)) {
            $this->error("删除失败");
        }
        $TeamModel = model('Team');
        $result = $TeamModel->where('id', intval($id))->delete();
        if ($result) {
            $this->success("删除成功");
        }
        $this->error("删除失败");
    }

}
